package mamikeji.liaowa.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import mamikeji.liaowa.R;
import mamikeji.liaowa.bean.DiaryDayBean;
import mamikeji.liaowa.bean.Image;
import mamikeji.liaowa.ui.activity.circle.PreviewCircleImgActivity;
import mamikeji.liaowa.widget.ninegrid.CustomImageView;
import mamikeji.liaowa.widget.ninegrid.NineGridlayout;
import mamikeji.liaowa.widget.ninegrid.ScreenTools;

/**
 * Created by 廉冲-android开发 on 2017/5/17.
 */
public class User_DiaryAdapter extends BaseAdapter {
    List<DiaryDayBean.DataBean> mList = new ArrayList<>();
    LayoutInflater inflater;
    Context context;
    OnItemClickListener mOnItemClickListener;
    public boolean bool_delete = false;

    public User_DiaryAdapter() {
    }

    public User_DiaryAdapter(List<DiaryDayBean.DataBean> mList, Context context) {
        this.mList = mList;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public interface OnItemClickListener {
        void onShareClick(int position);

        void onDeleteClick(int diary_id, int position);
    }

    public void setList(List<DiaryDayBean.DataBean> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_user_diary_msg, null);
            viewHolder = new ViewHolder();

            viewHolder.view_user_diary_state = convertView.findViewById(R.id.view_user_diary_state);
            viewHolder.textView_user_diary_time = (TextView) convertView.findViewById(R.id.textView_user_diary_time);
            viewHolder.textView_user_diary_title = (TextView) convertView.findViewById(R.id.textView_user_diary__title);
            viewHolder.iv_diary_More = (NineGridlayout) convertView.findViewById(R.id.iv_diary_More);
            viewHolder.iv_diary_One = (CustomImageView) convertView.findViewById(R.id.iv_diary_One);
            viewHolder.imageView_user_diary_share = (ImageView) convertView.findViewById(R.id.imageView_user_diary_share);

            viewHolder.relativeLayout_diary_delete = (RelativeLayout) convertView.findViewById(R.id.relativeLayout_diary_delete);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        final DiaryDayBean.DataBean dataBean = mList.get(position);


        viewHolder.view_user_diary_state.setBackgroundResource(position == 0 ? R.drawable.shape_diary_radio_sel : R.drawable.shape_diary_radio_null);
        viewHolder.textView_user_diary_title.setText(dataBean.getContent());

        if (dataBean.getImages() == null) {
            viewHolder.iv_diary_More.setVisibility(View.GONE);
            viewHolder.iv_diary_One.setVisibility(View.GONE);
        } else if (dataBean.getImages().size() == 0) {
            viewHolder.iv_diary_More.setVisibility(View.GONE);
            viewHolder.iv_diary_One.setVisibility(View.GONE);
        } else if (dataBean.getImages().size() == 1) {
            viewHolder.iv_diary_More.setVisibility(View.GONE);

            if (dataBean.getImages().get(0).equals("null") || dataBean.getImages().get(0).equals("")){
                viewHolder.iv_diary_One.setVisibility(View.GONE);
            }else {
                viewHolder.iv_diary_One.setVisibility(View.VISIBLE);
                handlerOneImage(viewHolder, new Image(dataBean.getImages().get(0),250,250));
            }
        } else {
            viewHolder.iv_diary_More.setVisibility(View.VISIBLE);
            viewHolder.iv_diary_One.setVisibility(View.GONE);

            List<String> lsists = new ArrayList<>();
            for (int i = 0 ;i<dataBean.getImages().size();i++){
                lsists.add(dataBean.getImages().get(i));
            }

            viewHolder.iv_diary_More.setImagesData(lsists);
        }

        viewHolder.iv_diary_More.setOnItemClickListener(new NineGridlayout.OnItemClickListener() {
            @Override
            public void onMultipleImgClick(int position) {
                ArrayList<String> stringList = new ArrayList<String>();
                for (int i = 0; i < dataBean.getImages().size(); i++) {
                    stringList.add(dataBean.getImages().get(i));
                }

                Intent intent = new Intent(context, PreviewCircleImgActivity.class);
                intent.putStringArrayListExtra("urlList", stringList);
                intent.putExtra("position", position);
                context.startActivity(intent);
            }
        });
        viewHolder.iv_diary_One.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ArrayList<String> stringList = new ArrayList<String>();
                for (int i = 0; i < dataBean.getImages().size(); i++) {
                    stringList.add(dataBean.getImages().get(i));
                }

                Intent intent = new Intent(context, PreviewCircleImgActivity.class);
                intent.putStringArrayListExtra("urlList", stringList);
                intent.putExtra("position", 1);
                context.startActivity(intent);
            }
        });

        viewHolder.imageView_user_diary_share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnItemClickListener.onShareClick(position);
            }
        });

        String[] times = dataBean.getCreate_time().split("T")[1].split(":");
        viewHolder.textView_user_diary_time.setText(times[0]+"："+times[1]);

        viewHolder.relativeLayout_diary_delete.setVisibility(bool_delete ? View.VISIBLE : View.GONE);
        viewHolder.relativeLayout_diary_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnItemClickListener.onDeleteClick(dataBean.getId(),position);
            }
        });
        return convertView;
    }

    public class ViewHolder {
        View view_user_diary_state;
        TextView textView_user_diary_time;
        TextView textView_user_diary_title;
        NineGridlayout iv_diary_More;
        CustomImageView iv_diary_One;
        ImageView imageView_user_diary_share;

        RelativeLayout relativeLayout_diary_delete;
    }

    private void handlerOneImage(ViewHolder viewHolder, Image image) {
        int totalWidth;
        int imageWidth;
        int imageHeight;
        ScreenTools screentools = ScreenTools.instance(context);
        totalWidth = screentools.getScreenWidth() - screentools.dip2px(80);
        imageWidth = screentools.dip2px(image.getWidth());
        imageHeight = screentools.dip2px(image.getHeight());
        if (image.getWidth() <= image.getHeight()) {
            if (imageHeight > totalWidth) {
                imageHeight = totalWidth;
                imageWidth = (imageHeight * image.getWidth()) / image.getHeight();
            }
        } else {
            if (imageWidth > totalWidth) {
                imageWidth = totalWidth;
                imageHeight = (imageWidth * image.getHeight()) / image.getWidth();
            }
        }
        ViewGroup.LayoutParams layoutparams = viewHolder.iv_diary_One.getLayoutParams();
        layoutparams.height = imageHeight;
        layoutparams.width = imageWidth;
        viewHolder.iv_diary_One.setLayoutParams(layoutparams);
        viewHolder.iv_diary_One.setClickable(true);
        viewHolder.iv_diary_One.setScaleType(ImageView.ScaleType.FIT_XY);
        viewHolder.iv_diary_One.setImageUrl(image.getUrl());

    }
}
